package com.sgb.kjwl.viewmodel.self;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.CarListEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.CarManagerActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CarListModel extends BaseViewModel {
    public CarManagerActivity activity;
    public SelfRepertory repertory;
    private int page = 1;
    private int limit = 10;

    public CarListModel(CarManagerActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }
    public void getList(boolean isMore){
      //  page = isMore ? page + 1 : 1;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
      /*  APIManager.getInstance().changeAPIManager(BuildConfig.LOCAL_TEST_URL_ZHANG);*/
        APIManager.getInstance().getDriverAPI().getCarList().enqueue(new Callback<BaseEntity<List<CarListEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarListEntity>>> call, Response<BaseEntity<List<CarListEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {

                        activity.showList(response.body().getData(),isMore);


                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarListEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
